package com.smh.demo1.a01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Locale;

@SpringBootApplication
@EnableAspectJAutoProxy // 开启aop
public class Demo1Application {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
        ConfigurableApplicationContext context = SpringApplication.run(Demo1Application.class, args);
//        Object demo1 = context.getBean("demo1");

/*
        Field field = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        field.setAccessible(true);
        ConcurrentHashMap<String,Object> singletonObjects = (ConcurrentHashMap)field.get(context.getBeanFactory());
        singletonObjects.entrySet().stream().filter(entry->entry.getKey().startsWith("component")).forEach(entry->{
            System.out.println(entry.getKey()+"-->"+entry.getValue());
        });*/

        // 就是对于一个"Code"进行翻译,指定语言环境,就会返回对应的值
        String message = context.getMessage("", null, null, Locale.CHINA);
        System.out.println(message);

        // 查找静态资源
        Resource[] resources = context.getResources("classpath*:META-INF/spring.factories"); // 加上*号,表示查找jar包中的文件
        for (Resource resource : resources) {
            System.out.println(resource);
        }

        // 获取环境变量,配置信息
        String property = context.getEnvironment().getProperty("server.port");
        System.out.println(property);
//        Map<String, Object> systemProperties =
//                context.getEnvironment().getSystemProperties();
//        systemProperties.forEach((key, value) -> {
//            System.out.println(key + "-->" + value);
//        });

        // 发布事件
//        context.publishEvent(new MyEvent(context));
        context.getBean(Component2.class).register();

        Thread.sleep(2000);

    }

}
